import matplotlib.pyplot as plt
import numpy as np

# 设置参数
beta = 2
p = 3
L = -1
U = 1

# 计算正规数和次正规数
regular_numbers = []
subnormal_numbers = []
for e in range(L, U + 1):
    for i in range(2):
        for j in range(2):
            val = (1 + 0.5 * i + 0.25 * j) * pow(beta, e)
            regular_numbers.append(val)
            regular_numbers.append(-val)
            if e == L and (i != 0 or j != 0):
                sub_val = (0 + 0.5 * i + 0.25 * j) * pow(beta, L)
                subnormal_numbers.append(sub_val)
                subnormal_numbers.append(-sub_val)

# 添加0到正规数列表
regular_numbers.append(0)

# 绘制正规数
plt.figure(figsize=(10, 2))
plt.scatter(regular_numbers, [0] * len(regular_numbers), color='blue', label='Regular Numbers')
plt.title('Regular Numbers in Floating Point System')
plt.xlabel('Value')
plt.yticks([])
plt.legend()
plt.show()

# 绘制扩展的浮点数系统（包括次正规数）
extended_numbers = regular_numbers + subnormal_numbers
plt.figure(figsize=(10, 2))
plt.scatter(extended_numbers, [0] * len(extended_numbers), color='red', label='Extended Numbers (including Subnormal)')
plt.title('Extended Floating Point System on Real Axis')
plt.xlabel('Value')
plt.yticks([])
plt.legend()
plt.show()
